package com.action.admin.goods;

import java.util.List;

import com.action.BaseAction;
import com.dao.GoodsDao;
import com.entity.Employee;
import com.entity.Good;
import com.entity.GoodsType;
import com.util.Page;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

/**
 * 
 * @author Administrator 商品信息
 * 
 */
// @ParentPackage("json-default")
@ParentPackage("extendsjson-default")
@Namespace("/goods")
@ResultPath("/WEB-INF/pages")
public class GoodsAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(GoodsAction.class);

	/**
	 * 增加功能
	 * 
	 * @return
	 */
	@Action(value = "addgoods", results = { @Result(name = "success", location = "goods_add.jsp") })
	public String addfunction() {
		Employee emp = (Employee) session("userinfo");
		if (emp == null)
			return LOGIN;
		return SUCCESS;
	}

	/**
	 * 修改功能
	 * 
	 * @return
	 */
	@Action(value = "updatefunction", results = { @Result(name = "success", location = "goods_update.jsp") })
	public String updatefunction() {
		Employee emp = (Employee) session("userinfo");
		if (emp == null)
			return LOGIN;
		return SUCCESS;
	}

	/**
	 * 保存功能
	 * 
	 * @return
	 */
	@Action(value = "savefunction", results = { @Result(name = "success", type = "json") })
	public String savefunction() {
		Employee emp = (Employee) session("userinfo");
		if (emp == null)
			return LOGIN;
		try {
			flag = goodsDao.save(goods);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@Action(value = "delfunction", results = { @Result(name = "success", type = "json") })
	public String delfunction() {
		Employee emp = (Employee) session("userinfo");
		if (emp == null)
			return LOGIN;
		try {
			flag = goodsDao.save(goods);
			functionlist();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 *列表
	 * 
	 * @return
	 */
	@Action(value = "goodslist", results = { @Result(name = "success", location = "goods_list.jsp") })
	public String functionlist() {
		Employee emp = (Employee) session("userinfo");
		if (emp == null)
			return LOGIN;
		try {
			page = goodsDao.findbyPage(page);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	private GoodsDao goodsDao = (GoodsDao) ioc.newInstance(GoodsDao.class);
	private boolean flag = false;
	private String stateus;
	private List list;
	private Good goods;
	private GoodsType goodstype;
	private Page page = new Page();

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public Good getGoods() {
		return goods;
	}

	public void setGoods(Good goods) {
		this.goods = goods;
	}

	public GoodsType getGoodstype() {
		return goodstype;
	}

	public void setGoodstype(GoodsType goodstype) {
		this.goodstype = goodstype;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getStateus() {
		return stateus;
	}

	public void setStateus(String stateus) {
		this.stateus = stateus;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
}
